package com.example.news.mapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;
import java.util.Set;

@Repository
public class RedisDaoImpl implements RedisDao{

    @Autowired
    private StringRedisTemplate redisTemplate;

    //返回符合条件的key的set集合
    @Override
    public Set<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    //根据key查询map
    @Override
    public Map<Object, Object> getAllMap(String key) {
        //redisTemplate.opsForHash()是以hash形式获取键值对，除此方法外还有4种方法
        //对应redis的5种数据类型
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public Integer hashIncr(String key, String hkey) {
        //用于给哈希表中的字段值加上指定增量值(可以加负值)
        return redisTemplate.opsForHash().increment(key, hkey, 1).intValue();
    }

    @Override
    public void zsetSetAll(String key, String value, double score) {
        //向zset中添加数据dasfadsfa啊啊啊
        //向zset中添加数据dasfadsfannn
        redisTemplate.opsForZSet().add(key,value,score);
    }

    @Override
    public Set<String> reverseRange(String key, long start, long end) {
        //将zset倒序排序
        return redisTemplate.opsForZSet().reverseRange(key, start, end);
    }

}
